﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Synergy
{
    public partial class Form修改密码 : Form
    {
        public string 工号 { get; set; }
        public Form修改密码()
        {
            InitializeComponent();
        }

        private void button修改密码_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(textBox工号.Text))
            {
                MessageBox.Show("请输入工号");
                return;
            }
            
            if (string.IsNullOrEmpty(textBox新密码.Text))
            {
                MessageBox.Show("请输入新密码");
                return;
            }
            //Z_UserPassword
            string sql = $"select * from Z_UserPassword where cUSER_Id = '{工号}'";
            DataTable dt = SqlSugarHelp.scope.Ado.GetDataTable(sql);
            if (dt != null && dt.Rows.Count > 0)
            {
                if (string.IsNullOrEmpty(textBox原密码.Text) == true)
                {
                    MessageBox.Show("请输入原始密码");
                    return;
                }

                sql = $"select * from Z_UserPassword where cUSER_Id = '{工号}' and password = '{textBox原密码.Text.Trim()}'";
                dt = SqlSugarHelp.scope.Ado.GetDataTable(sql);
                if (dt != null && dt.Rows.Count > 0)
                {
                    sql = $"delete  from Z_UserPassword where cUSER_Id = '{工号}' ";
                    SqlSugarHelp.scope.Ado.ExecuteCommand(sql);
                    sql = $"insert into Z_UserPassword (cUSER_Id,password) values ('{工号}','{textBox新密码.Text.Trim()}')";
                    int r = SqlSugarHelp.scope.Ado.ExecuteCommand(sql);
                    if (r > 0)
                    {
                        MessageBox.Show("修改成功");
                    }
                    else
                    {
                        MessageBox.Show("修改失败");
                    }
                }
                else
                {
                    MessageBox.Show("原密码输入错误");
                    return;
                }
            }
            else
            {
                sql = $"insert into Z_UserPassword (cUSER_Id,password) values ('{工号}','{textBox新密码.Text.Trim()}')";
                int r = SqlSugarHelp.scope.Ado.ExecuteCommand(sql);
                if (r > 0)
                {
                    MessageBox.Show("修改成功");
                }
                else
                {
                    MessageBox.Show("修改失败");
                }
            }
        }

        private void Form修改密码_Load(object sender, EventArgs e)
        {
            this.textBox工号.Text = 工号;
        }
    }
}
